//
//  VCOfCountry.m
//  project-A (pianke)
//
//  Created by dllo on 16/2/29.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "VCOfCountry.h"
#import <AFNetworking.h>
#import "ModelOfCountry.h"
#import "CellOfCountry.h"
#import "VCOfTrip.h"
#import "VCOfyouji.h"
#import "VCOfSpecial.h"
#import <DKNightVersion.h>
#import "VCofLVxingdi.h"



#define WIDTH [UIScreen mainScreen].bounds.size.width

#define HEIGHT  [UIScreen mainScreen].bounds.size.height

@interface VCOfCountry ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tabview;

@property (nonatomic,retain)NSMutableArray *array;

@end

@implementation VCOfCountry

-(void)dealloc{
    [_strurl release];
    [_array release];
    [_tabview release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self handleData];
    [self createTableView];
    self.view.backgroundColor = [UIColor orangeColor];
}


- (void)handleData {
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:self.strurl];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            self.array = [NSMutableArray array];
            for (NSDictionary *dic in responseObject) {
                ModelOfCountry *model = [[ModelOfCountry alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                model.idd = [NSString stringWithFormat:@"%@",[dic objectForKey:@"id"]];
                
                [self.array addObject:model];
                
                NSLog(@"%@",self.array);
                
                [model release];
            }
            [self.tabview reloadData];
        }
    }];
    [dataTask resume];
    
}

- (void)createTableView {
    self.tabview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tabview];
    self.tabview.delegate =self;
    self.tabview.dataSource = self;
    [self.tabview registerClass:[CellOfCountry class]forCellReuseIdentifier:@"CellOfCountry"];
    
    self.tabview.dk_backgroundColorPicker  = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.64 green:0.74 blue:0.86 alpha:1]);

    
    [self.tabview release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.array.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CellOfCountry *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfCountry"];
    
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 10;
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    
    cell.block = ^(NSString *strid,NSString *str) {
        if ([str isEqualToString:@"攻略"]) {
            VCOfyouji *vc = [[VCOfyouji alloc]init];
            vc.strurl = [NSString stringWithFormat:@"http://chanyouji.com/api/wiki/destinations/%@.json",strid];
            
        [self.navigationController pushViewController:vc animated:YES];
            
            
            [vc release];
            
            NSLog(@"攻略");
        } else if ([str isEqualToString:@"行程"]) {
            VCOfTrip *vc = [[VCOfTrip alloc]init];
            
            vc.strURL = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/plans/%@.json?page=1",strid];
            [self.navigationController pushViewController:vc animated:YES];
            NSLog(@"%@",vc.strURL);
            
            [vc release];
            
            NSLog(@"行程");
        } else if ([str isEqualToString:@"旅行地"]) {

            
            VCofLVxingdi*vc = [[VCofLVxingdi alloc] init];
            vc.strURL = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/attractions/%@.json?page=1",strid];
            vc.strId = strid;
            [self.navigationController pushViewController:vc animated:YES];
            [vc release];

            
            
            
            NSLog(@"旅行地");
        } else {
            VCOfSpecial *vc = [[VCOfSpecial alloc]init];
            vc.strurl = [NSString stringWithFormat:@"http://chanyouji.com/api/articles.json?destination_id=%@&page=1",strid];
            [self.navigationController pushViewController:vc animated:YES];
            
            [vc release];
            
            NSLog(@"专题");
        }
    };
    
    ModelOfCountry *model = [self.array objectAtIndex:indexPath.row];
    [cell passModel:model];
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return WIDTH * 0.80;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
